package cn.smartorigin.soas.frame;

import com.sun.awt.AWTUtilities; 

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LoginFrame extends JFrame{
	
	private ImageIcon loginButtonIcon = new ImageIcon("img/login/login_button.png");
    private ImageIcon loginButtonRoverIcon = new ImageIcon("img/login/login_button_rover.png");
    private ImageIcon loginButtonPressedIcon = new ImageIcon("img/login/login_button_pressed.png");
    private ImageIcon closeButtonIcon = new ImageIcon("img/login/login_close.png");
    private ImageIcon closeButtonRoverIcon = new ImageIcon("img/login/login_close_rover.png");
    private ImageIcon closeButtonPressedIcon = new ImageIcon("img/login/login_close_pressed.png");
    //private JButton btnLogin = createTransparentButton(loginButtonIcon, loginButtonRoverIcon, loginButtonPressedIcon);
    //private JButton btnClose = createTransparentButton(closeButtonIcon, closeButtonRoverIcon, closeButtonPressedIcon);
    private ImageButton btnClose = new ImageButton(closeButtonIcon, closeButtonRoverIcon, closeButtonPressedIcon);
    private ImageButton btnLogin = new ImageButton(loginButtonIcon, loginButtonRoverIcon, loginButtonPressedIcon);
    
    private ImageIcon logoIcon = new ImageIcon("img/login/login_logo.png");
    private ImageIcon logoRoverIcon = new ImageIcon("img/login/login_logo_rover.png");
    private ImageIcon leftTopIcon = new ImageIcon("img/login/login_left_top.png");
    private ImageIcon leftIcon = new ImageIcon("img/login/login_left.png");
    private ImageIcon rightIcon = new ImageIcon("img/login/login_right.png");
    
    private int width = loginButtonIcon.getIconWidth();
    private int height = loginButtonIcon.getIconHeight() + leftIcon.getIconHeight() + logoIcon.getIconHeight();
    
    
//    private JLabel logoLabel = createDraggableLabel(logoIcon);


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LoginFrame(){
		init();
	}
	
	private void init(){

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setUndecorated(true);								//设置无边缘
		AWTUtilities.setWindowOpaque(this, false);				//设置完全透明
		
		this.setTitle(" Login - SOAS");
		this.setSize(width, height);
		this.setBackground(null);
		
		this.setResizable(false);
		this.setLocationRelativeTo(null);

		
		JPanel centerPane = new JPanel(new BorderLayout());
		centerPane.add(btnLogin, BorderLayout.SOUTH);
		this.setContentPane(centerPane);
		centerPane.setOpaque(false);
		centerPane.setBackground(null);

		
		ImageIcon img = new ImageIcon("img/login/login_background.png"); 
		ImagePanel inputPane = new ImagePanel(img);
		
		inputPane.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 0));
		GridLayout gl =new GridLayout(4,2);
		gl.setVgap(40);
		inputPane.setLayout(gl);
	    inputPane.add(createInputLabel("Server:"));
	    inputPane.add(new CellTextField("localhost"));
	    inputPane.add(createInputLabel("Company:"));
	    inputPane.add(new CellTextField());
	    inputPane.add(createInputLabel("User Name:"));
	    inputPane.add(new CellTextField("admin"));
	    inputPane.add(createInputLabel("Password:"));
	    inputPane.add(new CellTextField());
//	    inputPane.add(createInputLabel("Language:"));


		
 //       JCheckBox cbRememberMe = new JCheckBox("Remember me");
 //       cbRememberMe.setOpaque(false);
 //       setupComponent(cbRememberMe);
 //      inputPane.add(cbRememberMe);
		
		JPanel topPane = new JPanel(new BorderLayout());
		topPane.setBackground(null);
//		topPane.add(logoLabel, BorderLayout.CENTER);
		topPane.add(btnClose, BorderLayout.EAST);
		//topPane.add(new ImageLabel(leftTopIcon), BorderLayout.WEST);
		topPane.add(new JLabel(logoIcon), BorderLayout.CENTER);
		topPane.add(new JLabel(leftTopIcon), BorderLayout.WEST);
		topPane.addMouseListener(moveWindowListener);
		topPane.addMouseMotionListener(moveWindowListener);
		
		centerPane.add(topPane, BorderLayout.NORTH);
		centerPane.add(inputPane, BorderLayout.CENTER);
				
		btnClose.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
	        }
	    });
		
		centerPane.add(new JLabel(this.leftIcon), BorderLayout.WEST);
        centerPane.add(new JLabel(this.rightIcon), BorderLayout.EAST);
        centerPane.add(topPane, BorderLayout.NORTH);
        centerPane.add(inputPane, BorderLayout.CENTER);
        
		this.setVisible(true);
	}
	
    private JButton createTransparentButton(ImageIcon icon, ImageIcon roverIcon, ImageIcon pressedIcon) {
        JButton button = new JButton();
        button.setBorder(null);
        button.setMargin(null);
        button.setOpaque(false);
        button.setIcon(icon);
        button.setRolloverEnabled(true);
        button.setRolloverIcon(roverIcon);
        button.setPressedIcon(pressedIcon);
        button.setContentAreaFilled(false);
        button.setFocusPainted(false);
        button.setRequestFocusEnabled(false);

        return button;
    }
    
    private void setupComponent(JComponent c) {
        c.setFont(SoasUtil.FONT_14_BOLD);
        c.setForeground(SoasUtil.DEFAULT_TEXT_COLOR);
    }
    
    private JLabel createInputLabel(String text) {
        JLabel label = new JLabel(text);
        setupComponent(label);
        return label;
    }
    
    private MouseAdapter moveWindowListener = new MouseAdapter() {

        private Point lastPoint = null;

        @Override
        public void mousePressed(MouseEvent e) {
            lastPoint = e.getLocationOnScreen();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            Point point = e.getLocationOnScreen();
            int offsetX = point.x - lastPoint.x;
            int offsetY = point.y - lastPoint.y;
            Rectangle bounds = LoginFrame.this.getBounds();
            bounds.x += offsetX;
            bounds.y += offsetY;
            LoginFrame.this.setBounds(bounds);
            lastPoint = point;
        }
};
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		//new LoginFrame();
		new MainFrame();
	}
}
